import 'package:flutter/material.dart';

/// 页面之间传递数据
/// 1. 创建一个信息类
/// 2. 展示列表
/// 3. 创建详情页面
/// 4. 跳转和传递数据到详情页

class SendDataApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final String appTitle = 'Send Data Demo';
    return MaterialApp(
      title: appTitle,
      theme: ThemeData(primarySwatch: Colors.red),
      home: SendDataAppPage(title: appTitle),
    );
  }
}

class SendDataAppPage extends StatefulWidget {
  final String title;

  SendDataAppPage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() => SendDataAppPageState();
}

class SendDataAppPageState extends State<SendDataAppPage> {
  final todos = List<Todo>.generate(
      20,
      (i) => Todo(
            'Todo $i',
            'A description of what needs to be done for Todo $i',
          ));

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
          itemCount: todos.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(todos[index].title),
              onTap: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) =>
                            DetailScreen(todo: todos[index])));
              },
            );
          }),
    );
  }
}

class DetailScreen extends StatelessWidget {
  final Todo todo;
  DetailScreen({Key key, @required this.todo}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("${todo.title}"),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Text("${todo.description}"),
      ),
    );
  }
}

class Todo {
  final String title;
  final String description;
  Todo(this.title, this.description);
}
